// pages/goodsOrder/newAddAres/index.js
import {
  addAddress
} from "../../../server/api";
import {areaList} from '../../../utils/areaList'
const app = getApp();
Page({
  data: {
    oss_url: app.globalData.oss_url,
    normalSwitch: false,
    areaList:[],
    show:false,
    province:'',
    area:'',
    city:'',
    address:'',
    name:'',
    mobile:'',
    id:''
  },
  onLoad: function (options) {
   console.log(options)
    this.setData({
      id:options.id,
      name:options.name,
      province:options.province,
      city:options.city,
      area:options.area,
      address:options.address,
      mobile:options.mobile
    })
  },
  onShow: function () {
    this.setData({
      areaList
    })
  },
  selectArea(){
    this.setData({
      show:true
    })
  },
  switchChange(e){
    console.log(e)
    this.setData({
      normalSwitch:e.detail.value
    })
  },
  confirm(e){
    console.log(e)
    this.setData({
      province:e.detail.values[0].name,
      city:e.detail.values[1].name,
      area:e.detail.values[2].name,
      show:false
    })

  },
  onClose(){
    this.setData({ show: false });
  },
  cancel(){
    this.setData({
      show:false
    })
  },
  getValue(e){
    // console.log(e)
    // console.log(e.detail.value)
    let type=e.currentTarget.dataset.type;
    switch(type){
      case 'name':
        this.setData({
          name:e.detail.value
        })
      break;
      case 'num':
        this.setData({
          mobile:e.detail.value
        })
      break;
      case 'address':
        this.setData({
          address:e.detail.value
        })
      break;
    }
  },
  save(){
    let data={
      oneId:app.globalData.user_info.oneId,
      receiverName:this.data.name,
      receiverPhone:Number(this.data.mobile),
      receiverProvince:this.data.province,
      receiverCity:this.data.city,
      receiverArea:this.data.area,
      receiverDetailAddress:this.data.address,
      defaultFlag:this.data.normalSwitch?1:0,
      receiverId:this.data.receiverId
    }
    console.log(data)
    addAddress(data).then(res=>{
      console.log(res)
      if(res.code==0){
        wx.navigateTo({
          url: '/pages/goodsOrder/myAddress/index',
        })
      }
    })
  },
  submit(){
      if(this.data.name==''){
        wx.showToast({
          title: '收货人不能为空',
          icon:'none'
        });
      }else if(this.data.mobile==''){
        wx.showToast({
          title: '手机号不能为空',
          icon:'none'
        });
      }else if(this.data.address==''){
        wx.showToast({
          title: '详细地址不能为空',
          icon:'none'
        });
      }
     this.save()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})